<?php


error_reporting(-1);
class SyncGoogleClass
{
	private $_requestArr = array();
	private $_localBasePath = '';
	private $_syncPath = '';
	private $_remoteBasePath = '';
	private $_dealArr = array();
	private $_dbTbl = '';
	private $_dbLink = false;
	private $_jsonData = '';
	
	public function __construct($requestJSON, $localBasePath, $syncPath = '/trunk/')
	{
		$localBasePath = str_replace('\\', '/', $localBasePath);
		$this->_syncPath = $syncPath = '/'.trim($syncPath, '/').'/';
		$this->_jsonData = $requestJSON;
		if(!is_array($this->_requestArr = $requestJSON = json_decode($requestJSON, true)) || empty($localBasePath) || !is_dir($localBasePath))
		{
			die('$requestJSON error.');
		}
		$requestJSON = $this->_requestArr = $requestJSON['revisions'][0];
		$this->_localBasePath = rtrim($localBasePath, '/').'/';
		list($this->_dealArr['added'], $this->_dealArr['modified'], $this->_dealArr['removed']) = array($requestJSON['added'], $requestJSON['modified'], $requestJSON['removed']);
		$this->_requestArr['url'] = rtrim($requestJSON['url'], '/');

		
	}
	
	public function start()
	{
		
	}
	
	public function getDealArr()
	{
		return $this->_dealArr;
	}
	
	public function setDB($db, $tbl, $dbLink)
	{
		$this->_dbTbl = $db.'.'.$tbl;
		$this->_dbLink = $dbLink;
	}
	
	public function writeDB($type)
	{
		list($added, $modified, $removed) = array(json_encode($this->_requestArr['added']), json_encode($this->_requestArr['modified']), json_encode($this->_requestArr['removed']));
		$sql = "INSERT IGNORE {$this->_dbTbl} (revision, `type`, added, modified, removed, author, msg, originalData, `addTime`)VALUES('{$this->_requestArr['revision']}', '{$type}', '{$this->_requestArr['revision']}', '{$added}', '{$modified}', '{$removed}', '{$this->_requestArr['author']}', '{$this->_requestArr['message']}', '{$this->_jsonData}', '{$this->_requestArr['timestamp']}')";
		$this->query($sql);
	}
	
	public function completeLastBadVersion($lastTime = 864000)
	{
		$sql = "SELECT originalData FROM {$this->_dbTbl} WHERE `addTime` >=".(time() - $lastTime)." AND upStatus='bad' ORDER BY addTime ASC LIMIT ,5";
		foreach($this->query($sql) as $v)
		{
			$v = json_decode($v['originalData'], true);
			$v = $v[0];
			
		}
	}
	
	public function getMaxVersion()
	{
		$sql = "SELECT MAX(revision) maxVersion FROM {$this->_dbTbl}";
		$rs = $this->query($sql);
		return $rs[0]['maxVersion'];
	}
	
	private function query($sql)
	{
		$___rs = array();
		$result = mysql_query($sql, $this->_dbLink);
		if(empty($result))
		{
			return array();
		}
		while($row = mysql_fetch_assoc($result))
		{
			$___rs[] = $row;
		}
		return $___rs;
	}
	
	public function getAbsPath($dealArr, $baseUrl = '', $syncPath = '')
	{
		$l = $r = array();
		$baseUrl = empty($baseUrl) ? $this->_requestArr['url'] : $baseUrl;
		$syncPath = empty($syncPath) ? $this->_syncPath : $syncPath;
		foreach($dealArr as $k=>$v)
		{
			$l[$k] = $this->_localBasePath.str_replace($syncPath, '', $v);
			$r[$k] = $baseUrl.$v;
		}
		return array($r, $l);
	}
	
	public function dealAdded($absPathArr)
	{
		print_r($absPathArr);
		foreach($absPathArr[0] as $v)
		{
			
		}
	}
}

$r='{"repository_path":"https://adt-bin.googlecode.com/svn/","project_name":"adt-bin","revisions":[{"added":["/trunk/static/common/onlineEditor/kindeditor/plugins/emoticons/images/111.gif","/trunk/static/common/onlineEditor/kindeditor/plugins/emoticons/images/112.gif","/trunk/static/common/onlineEditor/kindeditor/plugins/emoticons/images/113.gif","/trunk/static/common/onlineEditor/kindeditor/plugins/emoticons/images/114.gif","/trunk/static/common/onlineEditor/kindeditor/plugins/emoticons/images/115.gif","/trunk/static/common/onlineEditor/kindeditor/plugins/emoticons/images/116.gif","/trunk/static/common/onlineEditor/kindeditor/plugins/emoticons/images/117.gif","/trunk/static/common/onlineEditor/kindeditor/plugins/emoticons/images/118.gif","/trunk/static/common/onlineEditor/kindeditor/plugins/emoticons/images/119.gif","/trunk/static/common/onlineEditor/kindeditor/plugins/emoticons/images/120.gif","/trunk/static/common/onlineEditor/kindeditor/plugins/emoticons/images/121.gif","/trunk/static/common/onlineEditor/kindeditor/plugins/emoticons/images/122.gif","/trunk/static/common/onlineEditor/kindeditor/plugins/emoticons/images/123.gif","/trunk/static/common/onlineEditor/kindeditor/plugins/emoticons/images/124.gif","/trunk/static/common/onlineEditor/kindeditor/plugins/emoticons/images/125.gif","/trunk/static/common/onlineEditor/kindeditor/plugins/emoticons/images/126.gif","/trunk/static/common/onlineEditor/kindeditor/plugins/emoticons/images/127.gif","/trunk/static/common/onlineEditor/kindeditor/plugins/emoticons/images/128.gif","/trunk/static/common/onlineEditor/kindeditor/plugins/emoticons/images/129.gif","/trunk/static/common/onlineEditor/kindeditor/plugins/emoticons/images/130.gif","/trunk/static/common/onlineEditor/kindeditor/plugins/emoticons/images/131.gif","/trunk/static/common/onlineEditor/kindeditor/plugins/emoticons/images/132.gif","/trunk/static/common/onlineEditor/kindeditor/plugins/emoticons/images/133.gif","/trunk/static/common/onlineEditor/kindeditor/plugins/emoticons/images/134.gif","/trunk/static/common/onlineEditor/kindeditor/plugins/emoticons/images/static.gif"],"author":"5330056@qq.com","url":"http://adt-bin.googlecode.com/svn-history/r666/","timestamp":1344227466,"message":"[No log message]","path_count":25,"removed":[],"modified":[],"revision":666}],"revision_count":1}';
$x = new SyncGoogleClass($r, 'D:\temp');

$dealArr = $x->getDealArr();


print_r($x->getAbsPath($dealArr['added']));

//$x->getAbsPath();
//print_r(json_decode('{"repository_path":"https://adt-bin.googlecode.com/svn/","project_name":"adt-bin","revisions":[{"added":["/trunk/static/common/onlineEditor/kindeditor","/trunk/static/common/onlineEditor/kindeditor/attached","/trunk/static/common/onlineEditor/kindeditor/examples","/trunk/static/common/onlineEditor/kindeditor/php","/trunk/static/common/onlineEditor/kindeditor/plugins","/trunk/static/common/onlineEditor/kindeditor/plugins/anchor","/trunk/static/common/onlineEditor/kindeditor/plugins/anchor/anchor.js","/trunk/static/common/onlineEditor/kindeditor/plugins/baidumap","/trunk/static/common/onlineEditor/kindeditor/plugins/clearhtml","/trunk/static/common/onlineEditor/kindeditor/plugins/code"],"author":"5330056@qq.com","url":"http://adt-bin.googlecode.com/svn-history/r653/","timestamp":1344226592,"message":"[No log message]","path_count":10,"removed":[],"modified":[],"revision":653}],"revision_count":1}', true));


